package com.immunology.servlets;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.model.MappingPages;
import com.immunology.model.MedicineCard;
import com.immunology.model.Patient;
import com.immunology.service.CreateMedicineCardService;
import com.immunology.service.CreatePatientService;
import com.immunology.service.MedicineCardPostService;
import com.immunology.service.PatientRegistrationValidService;
import com.immunology.service.RegistrationPostService;

public class PatientRegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getServletContext().getRequestDispatcher(
				MappingPages.PATIENT_REGISTRATION_JSP).forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		@SuppressWarnings("unused")
		Patient patient = null;
		RegistrationPostService registrationPostService = new RegistrationPostService();
		registrationPostService.uploadRegistrationData(request, response);
		Map<String, String> registrationParameters = registrationPostService
				.getParameters();
		PatientRegistrationValidService registrationValidService = new PatientRegistrationValidService();
		MedicineCard medicineCard = null;
		MedicineCardPostService medicineCardPostService  = new MedicineCardPostService();
		//medicineCardPostService.uploadRegistrationData(request, response);
		boolean isValidParameter = registrationValidService.isValid(registrationParameters);
		Map<String, String> mapError = registrationValidService.getErrorMap();
		if (isValidParameter) {
			patient = new CreatePatientService().createPatient(registrationParameters);
			medicineCard = new CreateMedicineCardService().createMedicineCard(registrationParameters);
			request.getSession().setAttribute("patientRegistration",
					"Succsesfull Patient Registration");
			response.sendRedirect(MappingPages.DOCTOR_PAGE);
		} else {
			response.setCharacterEncoding("UTF-8");
			request.setAttribute("mapError", mapError);
			getServletContext().getRequestDispatcher(
					MappingPages.PATIENT_REGISTRATION_JSP).forward(request, response);
		}
	}

}
